// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network.packets;

import java.nio.ByteBuffer;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoPlayer;
import zombie.characters.BodyDamage.BodyPart;
import zombie.characters.skills.PerkFactory;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.debug.DebugLog;
import zombie.inventory.InventoryItem;
import zombie.network.GameClient;
import zombie.network.PacketValidator;
import zombie.network.packets.hit.Player;
import zombie.network.packets.hit.PlayerBodyPart;
import zombie.network.packets.hit.PlayerItem;

public class Stitch implements INetworkPacket {
    protected final Player wielder = new Player();
    protected final Player target = new Player();
    protected PlayerBodyPart bodyPart = new PlayerBodyPart();
    protected PlayerItem item = new PlayerItem();
    protected float stitchTime = 0.0F;
    protected boolean doIt = false;
    protected boolean infect = false;

    public void set(IsoGameCharacter _wielder, IsoGameCharacter _target, BodyPart _bodyPart, InventoryItem _item, boolean _doIt) {
        this.wielder.set(_wielder);
        this.target.set(_target);
        this.bodyPart.set(_bodyPart);
        if (_item != null) {
            this.item.set(_item);
        }

        this.stitchTime = _bodyPart.getStitchTime();
        this.doIt = _doIt;
        this.infect = _bodyPart.isInfectedWound();
    }

    @Override
    public void parse(ByteBuffer b, UdpConnection connection) {
        this.wielder.parse(b, connection);
        this.wielder.parsePlayer(connection);
        this.target.parse(b, connection);
        this.target.parsePlayer(null);
        this.bodyPart.parse(b, this.target.getCharacter());
        this.item.parse(b, connection);
        this.stitchTime = b.getFloat();
        this.doIt = b.get() == 1;
        this.infect = b.get() == 1;
    }

    @Override
    public void write(ByteBufferWriter b) {
        this.wielder.write(b);
        this.target.write(b);
        this.bodyPart.write(b);
        this.item.write(b);
        b.putFloat(this.stitchTime);
        b.putBoolean(this.doIt);
        b.putBoolean(this.infect);
    }

    public void process() {
        int int0 = this.wielder.getCharacter().getPerkLevel(PerkFactory.Perks.Doctor);
        if (!this.wielder.getPlayer().isAccessLevel("None")) {
            int0 = 10;
        }

        byte _byte = 20;
        if (this.doIt) {
            if (this.wielder.getCharacter().getInventory().contains("SutureNeedleHolder") || this.item.getItem().getType().equals("SutureNeedle")) {
                _byte = 10;
            }
        } else {
            _byte = 5;
        }

        if (this.wielder.getCharacter().getCharacterTraits().Hemophobic.isSet()) {
            this.wielder.getCharacter().getStats().setPanic(this.wielder.getCharacter().getStats().getPanic() + 50.0F);
        }

        if (this.item.getItem() != null) {
            this.item.getItem().Use();
        }

        if (this.bodyPart.getBodyPart().isGetStitchXp()) {
            this.wielder.getCharacter().getXp().AddXP(PerkFactory.Perks.Doctor, 15.0F);
        }

        this.bodyPart.getBodyPart().setStitched(this.doIt);
        int int1 = _byte - int0 * 1;
        if (int1 < 0) {
            int1 = 0;
        }

        if (!this.wielder.getPlayer().isAccessLevel("None")) {
            this.bodyPart.getBodyPart().setAdditionalPain(this.bodyPart.getBodyPart().getAdditionalPain() + (float)int1);
        }

        if (this.doIt) {
            this.bodyPart.getBodyPart().setStitchTime(this.stitchTime);
        }

        if (this.infect) {
            this.bodyPart.getBodyPart().setInfectedWound(true);
        }
    }

    @Override
    public boolean isConsistent() {
        return this.wielder.getCharacter() != null
            && this.wielder.getCharacter() instanceof IsoPlayer
            && this.target.getCharacter() != null
            && this.target.getCharacter() instanceof IsoPlayer
            && this.bodyPart.getBodyPart() != null
            && this.stitchTime < 50.0F
            && this.stitchTime >= 0.0F;
    }

    public boolean validate(UdpConnection connection) {
        if (!GameClient.bClient || !this.doIt || this.bodyPart.getBodyPart().isDeepWounded() && !this.bodyPart.getBodyPart().haveGlass()) {
            if (GameClient.bClient && !this.doIt && !this.bodyPart.getBodyPart().stitched()) {
                DebugLog.General.warn(this.getClass().getSimpleName() + ": Validate error: " + this.getDescription());
                return false;
            } else {
                return PacketValidator.checkShortDistance(connection, this.wielder, this.target, this.getClass().getSimpleName());
            }
        } else {
            DebugLog.General.warn(this.getClass().getSimpleName() + ": Validate error: " + this.getDescription());
            return false;
        }
    }

    @Override
    public String getDescription() {
        String string = "\n\t" + this.getClass().getSimpleName() + " [";
        string = string + "wielder=" + this.wielder.getDescription() + " | ";
        string = string + "target=" + this.target.getDescription() + " | ";
        string = string + "bodyPart=" + this.bodyPart.getDescription() + " | ";
        string = string + "item=" + this.item.getDescription() + " | ";
        string = string + "stitchTime=" + this.stitchTime + " | ";
        string = string + "doIt=" + this.doIt + " | ";
        return string + "infect=" + this.infect + "] ";
    }
}
